package com.xhu.servlet;

import com.xhu.bean.pojo.SalesPlan;
import com.xhu.service.SpService;
import com.xhu.service.impl.SpServiceImpl;
import com.xhu.utils.BaseServlet;
import com.xhu.utils.FormatUtils;
import com.xhu.utils.ResultData;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@WebServlet("/SpServlet/*")
public class SpServlet extends BaseServlet {
    public ResultData findAll(HttpServletRequest request){
        SpService spService=new SpServiceImpl();
        List<SalesPlan> list=spService.findAll();
        return ResultData.ok().data(list);
    }

    public ResultData add(HttpServletRequest request){


        String emp_name=request.getParameter("planner_name");
        String planner_name=request.getParameter("planner_name");
        String saleplan_code=request.getParameter("saleplan_code");
        String planner_text=request.getParameter("planner_text");
        String planner_month=request.getParameter("planner_month");


        SalesPlan salesPlan=new SalesPlan();
        salesPlan.setPlanner_text(planner_text);    //获取计划内容
        salesPlan.setEmp_name(emp_name);            //设置员工
        salesPlan.setPlanner_month(planner_month);  //设置月份
        salesPlan.setSaleplan_code(saleplan_code);  //设置计划编码
        salesPlan.setPlanner_name(planner_name);    //设置计划人
        salesPlan.setCurrent_name(emp_name);        //设置
        salesPlan.setDispose_name(emp_name);
        salesPlan.setLast_time(new Date());

        SpService spService=new SpServiceImpl();
        boolean tf=spService.add(salesPlan);
        return ResultData.ok().msg(tf?"添加成功":"添加失败");
    }

    public ResultData list(HttpServletRequest request){
        Integer page=FormatUtils.stringFormatInteger(request.getParameter("page"));
        Integer limit=FormatUtils.stringFormatInteger(request.getParameter("limit"));
        //查询
        //String name=FormatUtils.formatString(request.getParameter("name"));

        SpService spService=new SpServiceImpl();
        List<SalesPlan> list=spService.queryPage(page,limit);

        return ResultData.ok().data(list);
    }

    public ResultData list_search(HttpServletRequest request){
        Integer page=FormatUtils.stringFormatInteger(request.getParameter("page"));
        Integer limit=FormatUtils.stringFormatInteger(request.getParameter("limit"));
        //查询
        //String name=FormatUtils.formatString(request.getParameter("name"));
        String search_name=request.getParameter("search_name");

        SpService spService=new SpServiceImpl();
        if(search_name==""){
            List<SalesPlan> list=spService.queryPage(page,limit);
            return ResultData.ok().data(list);
        }else{
            List<SalesPlan> list=spService.queryPage(page,limit,search_name);
            return ResultData.ok().data(list);
        }


    }

    public ResultData subm(HttpServletRequest request){

        String saleplan_id=request.getParameter("saleplan_id");

        System.out.println("subm里面获得id是"+saleplan_id);

        SpService spService=new SpServiceImpl();
        boolean tf=spService.subm(saleplan_id);
        return ResultData.ok().msg(tf?"提交成功":"提交失败");
    }

    public ResultData upData(HttpServletRequest request){

        String saleplan_id=request.getParameter("saleplan_id");

        String planner_name=request.getParameter("planner_name");
        String saleplan_code=request.getParameter("saleplan_code");
        String planner_text=request.getParameter("planner_text");
        String planner_month=request.getParameter("planner_month");


        SalesPlan salesPlan=new SalesPlan();
        salesPlan.setPlanner_text(planner_text);    //获取计划内容
        salesPlan.setPlanner_month(planner_month);  //设置月份
        salesPlan.setSaleplan_code(saleplan_code);  //设置计划编码
        salesPlan.setPlanner_name(planner_name);    //设置计划人

        SpService spService=new SpServiceImpl();
        boolean tf=spService.upData(salesPlan,saleplan_id);
        return ResultData.ok().msg(tf?"更新成功":"更新失败");

    }

    public ResultData del(HttpServletRequest request){
        String saleplan_id=request.getParameter("saleplan_id");

        System.out.println("del中的:"+saleplan_id);
        SpService spService=new SpServiceImpl();
        boolean tf=spService.del(saleplan_id);
        return ResultData.ok().msg(tf?"删除成功":"删除失败");

    }
}
